;(function($){
	$.fn.spa_discountDetail = function(opts){
		var el = this;
		var opt = {
			no : "",
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		var t = this;
		this.initHtml();
		this.getDiscount(function(data){
			t.initDiscountHtml(data);
			t.bindEvent();
		})
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//关闭
			el.find("#close-dialog,#modalBtnCancel").unbind("click").click(function(){
				el.modal("hide");
				el.empty();
				param.closeBack();
			});
			
			//确认
			el.find("#modalBtnSave").unbind("click").click(function(){
				var ct_brzd = {"no":param.no,
								"discode":el.find("#coupon_type input").attr("data-code"),
								"disrate":el.find("#coupon_rate input").val()};
    			t.saveOper(ct_brzd);
			});
			
			//取消折扣
			el.find("#modalBtnCancelDiscount").unbind("click").click(function(){
				ct_brzd = {"no":param.no,
							"discode":"",
							"disrate":"1.00",
							"str4":""};
				t.saveOper(ct_brzd);
			});
			
			//选择
    		el.find("#frm").on("click","button",function(){
    			if(!$(this).hasClass("disabled")){
    				var data = JSON.parse($(this).attr("data-json"));
        			var id = $(this).parents(".input-group").attr("id");
        			el.find(".modal-list .orderTaste").empty();
        			if(data.length > 0){
        				if(Math.ceil(data.length / 4) > 4){
            				el.find(".modal-list .orderTaste").css({"width":504});
            			}else{
            				el.find(".modal-list .orderTaste").css({"width":578});
            			}
    					$(data).each(function(key,value){
        					var btn_class = "btn-light";
//            					if(item == value.mc){
//        							btn_class = "btn-primary";
//        						}
        					if(id == "coupon_type"){
            					var btn_html = $('<button type="button" class="btn btn-app '+ btn_class +'" data-fixed="'+ value.fixed +'" data-rate="" data-code="'+ value.discode +'">'+ value.text +'</button>');
            					if(!!value.rate){
            						btn_html.attr("data-rate",value.rate);
            					}
            					if(value.text.length > Math.floor(((el.find(".orderTaste").width() - 16) /4) / 18)){
            						btn_html.css("font-size",((el.find(".orderTaste").width() - 16) / 4) / value.text.length);
            					}
            					el.find(".modal-list .orderTaste").append(btn_html).attr("data-type","type");
        					}else if(id == "coupon_rate"){
        						var btn_html = $('<button type="button" class="btn btn-app '+ btn_class +'">'+ value.descript1 +'</button>');
        						if(value.descript1.length > Math.floor(((el.find(".orderTaste").width() - 16) /4) / 18)){
            						btn_html.css("font-size",((el.find(".orderTaste").width() - 16) / 4) / value.descript1.length);
            					}
        						el.find(".modal-list .orderTaste").append(btn_html).attr("data-type","rate");
        					}else if(id == "coupon_remark"){
        						var btn_html = $('<button type="button" class="btn btn-app '+ btn_class +'">'+ value.descript1 +'</button>');
        						if(value.descript1.length > Math.floor(((el.find(".orderTaste").width() - 16) /4) / 18)){
            						btn_html.css("font-size",((el.find(".orderTaste").width() - 16) / 4) / value.descript1.length);
            					}
        						el.find(".modal-list .orderTaste").append(btn_html).attr("data-type","remark");
        					}
            			});
        				el.find(".modal-list button").css({"width":(el.find(".orderTaste").width() - 16) / 4,"height":44.5});
        			}else{
        				if($(this).parents(".input-group").attr("id") == "coupon_type"){
        					jf_alert("无折扣类型可选~");
    					}else if($(this).parents(".input-group").attr("id") == "coupon_rate"){
    						jf_alert("无折扣率可选~");
    					}else if($(this).parents(".input-group").attr("id") == "coupon_remark"){
    						jf_alert("无折原因可选~");
    					}
        			}
    			}
    		});
    		
    		//选择赋值
    		el.find(".orderTaste").on("click","button",function(){
    			if($(this).parents(".orderTaste").attr("data-type") == "type"){
    				el.find("#coupon_type input").val($(this).text());
    				if($(this).attr("data-fixed") == "1"){
    					el.find("#coupon_rate button").removeClass("disabled");
    					if(!!$(this).attr("data-rate")){
    						el.find("#coupon_rate input").val($(this).attr("data-rate"));
    					}
    					el.find("#coupon_type input").attr("data-code",$(this).attr("data-code"));
    				}else{
    					el.find("#coupon_rate button").addClass("disabled");
    				}
    			}else if($(this).parents(".orderTaste").attr("data-type") == "rate"){
    				el.find("#coupon_rate input").val($(this).text());
    			}else if($(this).parents(".orderTaste").attr("data-type") == "remark"){
    				el.find("#coupon_remark textarea").val($(this).text());
    			}
    			el.find(".orderTaste").empty();
    		});
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="mykb-box" id="mykeyboard"></div>'+
						'<div id="modal-dialog" class="modal-dialog" style="width:600px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp("折扣")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="min-height: 100px;">'+
									'<form id="frm" class="form-horizontal">'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("折扣类型:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" id="coupon_type" style="width:360px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="discode">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="pay_btn">'+
															'<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>'+
															$.i18n.propJsp("选择")+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("折扣率:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" id="coupon_rate" style="width:360px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="disrate">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="ar_search">'+
															'<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>'+
															$.i18n.propJsp("选择")+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</form>'+
									'<div class="widget-box transparent modal-list">'+  
										'<div class="orderTaste"></div>'+
									'</div>'+	
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancelDiscount" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+
													$.i18n.propJsp("取消折扣")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnSave" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+
													$.i18n.propJsp("确认")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-times"></i>'+
													$.i18n.propJsp("返回")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		getDiscount : function(back){
			var el = this.el;
			var param = this.param;
			var t = this;
			t.wait();
			var arg = {arg:{no:param.no}}
			jQuery.ajax({  
	    		type : 'POST',
	    		url :  $.HSAPI_AJAXURL+"/qms/mobile/procedure2",      
	    		data : $.HSAPI_ASKJSONINFO( JSON.stringify(arg),'POSDiscountDefault',"{}",""),
	    		dataType : 'json',  
	    		success : function(data){
	    			t.goon();
	    			if(data.RETURN&&data.RETURN!=1){
	    				jf_alert(data.MSG);
	    			}else{
	    				back(data);
	    			}
	    		},  
	    		error : function(data){
	    			t.goon();
	    			jf_alert(JSON.stringify(data));
	    		}  
	    	});
		},
		initDiscountHtml : function(data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			if(data.ct_brzd.length > 0){
    			if(!!data.ct_brzd[0].distext){
        			el.find("#coupon_type input").val(data.ct_brzd[0].distext);
        		}
        		if(!!data.ct_brzd[0].disrate){
        			el.find("#coupon_rate input").val(data.ct_brzd[0].disrate);
        		}
    		}
			
			el.find("#coupon_type button").attr("data-json",JSON.stringify(data.ct_discode));
			el.find("#coupon_rate button").attr("data-json",JSON.stringify(data.discoderate));
		},
		saveOper : function(ct_brzd){
    		var t=this;
    		var el=this.el;
    		var param=this.param;
    		
    		t.wait();
    		var postData={};
			postData.ct_brzd = ct_brzd;
    		$.ajax({
    			url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",      
    			type : "post",
    			dataType : "json",
    			data : $.HSAPI_ASKJSONINFO(JSON.stringify(postData),'POSCT_brzdEditSave',"{}"),
    			success : function(data){
    				t.goon();
					if(data.RETURN != -1){
    					param.dataBack();
    					el.modal("hide");
    					el.empty();
    				}else{
    					jf_alert(data.MSG);
    				}
    			},
    			error : function(data){
    				t.goon();
        			jf_alert(JSON.stringify(data));
    			},
    		});
    	},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
			
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
	}
	
	function checkTwoPoint(value){  		
	    value=value.toString()+".00";
	    value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
	    return value;
	}
	
})(jQuery);